package com.dps.omc.action;

import com.dps.omc.model.Artifact;
import com.dps.omc.model.LogFile;
import com.dps.omc.service.IArtifactService;
import com.dps.omc.service.ILogFileService;
import com.mongodb.gridfs.GridFSDBFile;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lip on 2016-01-19.
 */
@Controller
public class MongoFileController {

    private static final Logger logger = LoggerFactory.getLogger(MongoFileController.class);
    @Autowired
    private GridFsTemplate gridFsTemplate;
    @Autowired
    private IArtifactService artifactService;
    @Autowired
    private ILogFileService logFileService;

    @RequestMapping(value="/file/{artifactId}", method=RequestMethod.GET)
    public void handleFileDownload(@PathVariable String artifactId, HttpServletRequest request, HttpServletResponse response){

        Artifact artifact = artifactService.fetch(artifactId);

        GridFSDBFile file = gridFsTemplate.findOne(new Query().addCriteria(Criteria.where("filename").is(artifact.getPath())));
        if (file != null) {
            try {
                response.setContentType(file.getContentType());
                response.setContentLength((new Long(file.getLength()).intValue()));
                response.setHeader("content-Disposition", "attachment; filename=" + file.getFilename());// "attachment;filename=test.xls"
                // copy it to response's OutputStream
                IOUtils.copyLarge(file.getInputStream(), response.getOutputStream());
            } catch (IOException ex) {
                logger.error("IOError writing file to output stream");
            }
        }



        //return (MultipartFile) file;
    }

    @RequestMapping(value="/log/{logFileId}", method=RequestMethod.GET)
    public void logDownload(@PathVariable String logFileId, HttpServletRequest request, HttpServletResponse response){

        LogFile logFile = logFileService.fetch(logFileId);

        GridFSDBFile file = gridFsTemplate.findOne(new Query().addCriteria(Criteria.where("filename").is(logFile.getPath())));
        if (file != null) {
            try {
                response.setContentType(file.getContentType());
                response.setContentLength((new Long(file.getLength()).intValue()));
                response.setHeader("content-Disposition", "attachment; filename=" + file.getFilename());// "attachment;filename=test.xls"
                // copy it to response's OutputStream
                IOUtils.copyLarge(file.getInputStream(), response.getOutputStream());
            } catch (IOException ex) {
                logger.error("IOError writing file to output stream");
            }
        }

        //return (MultipartFile) file;
    }
}
